<%-- 
    Document   : InsertAuction
    Created on : Jul 20, 2013, 8:17:14 PM
    Author     : Ndnam
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Register New User</title>
    </head>
    <body>
        <form method="POST">
            Username <input type="text" name="username" value="${param.username}"><br>
            Firstname <input type="text" name="firstname" value="${param.firstname}"><br>
            Lastname <input type="text" name="lastname" value="${param.lastname}"><br>
            Email <input type="text" name="email" value="${param.email}"><br>
            Password <input type="password" name="password"><br>
            Confirm Password <input type="password" name="password2"><br>
            UserType <input type="text" name="usertype"><br>
            Address1 <input type="text" name="address1"  value="${param.address1}"><br>
            Address2 <input type="text" name="address2"><br>
            State <input type="text" name="state" value="${param.state}"><br>
            ZipCode <input type="text" name="zipcode" value="${param.zipcode}"><br>
            Country <input type="text" name="country"><br>
            PhoneNumber <input type="text" name="phonenumber" value="${param.phonenumber}"><br>
            <input type="Submit" value="OK" >
        </form>
    </body>
</html>


<font color=\"red\">
<jsp:useBean id="user" class="classes.User"/>
<jsp:setProperty name="user" property="*"/>
<jsp:useBean id="auctionDAO" class="classes.AuctionDAO"/>

<%
    //Input validation
    Boolean valid = true;
    String errorMessage = "";
    if (request.getParameter("username") != null) {
        if (!request.getParameter("username").matches("[a-zA-Z0-9]{6,20}")) {
            valid = false;
            errorMessage = "Username must have at least 6 characters and do not contain special chracters <br>";
        } else if (auctionDAO.searchUserByUsername(request.getParameter("username")) != null) {
            valid = false;
            errorMessage = "Username already existed <br>";
        }
        if (!request.getParameter("firstname").matches("[a-zA-Z\\s]{1,30}")) {
            valid = false;
            errorMessage += "Firstname and must not be empty and contain only letters and whitespaces <br>";
        }
        if (!request.getParameter("lastname").matches("[a-zA-Z ]{1,30}")) {
            valid = false;
            errorMessage += "Lastname and must not be empty and contain only letters and whitespaces <br>";
        }
        if (!request.getParameter("email").matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$")
            || request.getParameter("email").length() >40 ) {
            valid = false;
            errorMessage += "Invalid email address <br>";
        } else if (auctionDAO.searchUserByEmail(request.getParameter("email")) != null) {
            valid = false;
            errorMessage += "Email address already existed <br>";
        }
        if (!request.getParameter("password").matches(".{6,30}")) {
            valid = false;
            errorMessage += "Password must have at least 6 characters <br>";
        } else if (!request.getParameter("password").equals(request.getParameter("password2"))){
            valid = false;
            errorMessage += "Passwords do not match <br>";
        }
        if (!request.getParameter("address1").matches(".{6,50}")) {
            valid = false;
            errorMessage += "Address is invalid <br>";
        } 
        if (!request.getParameter("state").matches(".{4,20}")) {
            valid = false;
            errorMessage += "Invalid value <br>";
        } 
        if (!request.getParameter("zipcode").matches("[0-9]{4,10}")) {
            valid = false;
            errorMessage += "Invalid zipcode <br>";
        } 
        errorMessage += "You must select your country <br>";
        if (!request.getParameter("phonenumber").matches("[0-9]{8,12}")) {
            valid = false;
            errorMessage += "Invalid phone number <br>";
        } 
        
        //Display Error message
        out.println(errorMessage);

        //Insert to Database
        if (valid) {
            try {
                auctionDAO.insertUser(user);
                RequestDispatcher dispatch = request.getRequestDispatcher("Home.jsp");
                dispatch.forward(request, response);
                out.println("New User added successfully!");
            } catch (Exception ex) {
                out.println("FAILED");
            }
            out.println("OK");
        } else {
            out.println("Validation Failed");
        }
    }

%>
<font>